// SlagCar/ApproveView/ApproveView.js
let listBehavior = require('../../behavior/list')
const req = require('../../utils/request');
Page({
  // behaviors: [ listBehavior, ],
  /**
   * 页面的初始数据
   */
  data: {
    system_id: wx.getStorageSync('system_id') || 1164,  //231215 yan 图片字段只返回文件名时，需要系统号拼接图片地址
    baseURL: req.baseURL,

    tabList: ['审核中','已审核'], //顶部标签
    tabActive: 0, //标签选中索引

    title: '运输列表',
    baseQuery: [],

    filterList: ['全部','审核中','已审核'],
    filter: '全部',  //过滤选中按钮： 全部、审核中、已审核、退回
    page0: 1,
    total0: 0,
    limit0: 10,
    page1: 1,
    total1: 0,
    limit1: 10,
    scrollReset0: false,
    scrollReset1: false,

    activeNames: [],  //折叠面板
    deliveryList: [],  //统计工地列表

    driver_end_time_start: '',  //开始时间
    driver_end_time_end: '',  //结束时间
    curShowType: '',
    showCalendarStart: false,
    showCalendarEnd: false,

  },

  onTabTap(e){
    let { i, } = e.currentTarget.dataset;
    this.setData({
      tabActive: i,
    });
    if (i === 1 && !this.data.list1) this.getDataList();
  },
  //获取数据
  getDataList(){
    let {page0,limit0,page1,limit1,filter,list0,list1,baseQuery,tabActive} = this.data
    let total = 0,page,limit,list,
      obj = {};
    let url = 'DynamicQuery/query_dynamic_v3',
      where = [...baseQuery];

    if (tabActive === 0){
      page = page0;
      limit = limit0;
      list = list0;
      where.push(
        {CompareWith:"muck_waybill_main.is_auditing_1",Operator:"=",CompareValue:false,"or":0},
        // {CompareWith: "muck_waybill_main.auditing_1_time", Operator: "is null", CompareValue: "", or: 0}
      )
    }
    if (tabActive === 1){
      page = page1;
      limit = limit1;
      list = list1;
      where.push(
        {CompareWith:"muck_waybill_main.is_auditing_1",Operator:"=",CompareValue:true,"or":0},
      )
    }
    let post = {"oby":"muck_waybill_main.row_id","table_id":"muck_waybill_main",page,limit};
    if (where.length) post.SearchWhere = {filters: where,type: 'and'};
    wx.$post(3,url,post).then(res => {
      if (res.errcode != 0) return;
      if (res.data.row){
        total = res.data.total;
        let rows = res.data.row;
        if(page !== 1) rows = [ ...list,...rows, ]

        obj = {['total' + tabActive]: total,['list' + tabActive]: rows,['page' + tabActive]: page};
      }
      this.setData(obj);
    });
  },

  // 获取第一页
  getFirstPage() {
    let {tabActive} = this.data;
    this.data['page' + tabActive] = 1
    this.setData({
      ['scrollReset' + tabActive]: !this.data['scrollReset' + tabActive],
    })
    this.getDataList()
  },
  // m-list-box tobottom事件滚动到底部，获取下一页数据
  getNextPage() {
    let {tabActive} = this.data;
    this.data['page' + tabActive]++
    this.getDataList()
  },
  // 重新刷新第一页数据
  getRefreshPage() {
    let {tabActive} = this.data;
    this.data['page' + tabActive] = 1
    this.getDataList()
  },


  //预览照片
  previewImg(e){
    let {list,i} = e.currentTarget.dataset;
    list = list && list.split(',') || []
    let {  system_id,baseURL } = this.data,
      imgs = [];
    //231221 yan 修改 图片字段只返回文件名称，通过系统号拼接图片访问地址
    list.forEach(item => {
      let name = item.substring(item.lastIndexOf('/')+1);
      imgs.push(`${baseURL}/files/${system_id}/${name}`)
    });
    //231221 end
    wx.previewImage({
      current: imgs[i],
      urls: imgs
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let userType = wx.getStorageSync('user_type'),
      baseQuery = [];
    if (userType === 'driver'){
      let driver_row_id = wx.getStorageSync('driver_row_id')
      baseQuery = [
        {CompareWith: "muck_waybill_main.reference1_driver", Operator: "=", CompareValue: driver_row_id, or: 0}
      ];
    }
    let approveQuery = wx.getStorageSync('approveQuery');
    wx.removeStorageSync('approveQuery');
    if (approveQuery) baseQuery = [...baseQuery,...approveQuery];

    this.setData({baseQuery,userType })
    this.getDataList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
